package cc.service.impl;

import cc.dao.UserMapper;

import cc.entity.User;

import cc.entity.UserPassowrdEdit;

import cc.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;





/**

 * TODO

 *

 * @author My

 * @date 2021/10/11 17:11

 */

@Service

public class UserServiceImpl implements IUserService {



    @Autowired

    UserMapper userMapper;



    @Override

    public int save(User user) throws Exception {

        return userMapper.add(user);

    }



    @Override

    public User getById(Integer id) throws Exception {

        return userMapper.getById(id);

    }



    @Override

    public User getById2(Integer id) {

        return userMapper.selectById(id);

    }



    @Override

    public User update(UserPassowrdEdit userPassowrdEdit) {

        User user = userMapper.getById(userPassowrdEdit.getId());

        if(user != null){

            if(user.getPassword().equals(userPassowrdEdit.getOldPassword())){

                if(userPassowrdEdit.getOldPassword().equals(userPassowrdEdit.getNewPassword())){

                    return null;

                }else {

                    // 旧密码验证正确，新旧密码不同，可以更新

                    user.setPassword(userPassowrdEdit.getNewPassword());

                }

            }else {

                return null;

            }

        }

        userMapper.updatePassword(user);



        return user;

    }



}

